/**
 * @file ApplicationInfo.cs
 * @author Zane Mayo
 * @date 6/5/2009
 */

using System;

namespace MxitAPI {
	
	/**
	 * Holds application wide data such as the Version and Features
	 */
	public class ApplicationInfo {

        private const int productIdLength = 46;

        public event ProtocolMessageErrorEventHandler ProtocolMessageError;

		private static ApplicationInfo application;

        private string productId;

		public string Version { get; set; }
		public string Capabilities { get; set; }
		public int Features { get; set; }
		public string DialingCode { get; set; }
		public string Locale { get; set; }
		public int MaxSupportedVersion { get; set; }  /// Returned by Mxit at login 
        public string DistributionCode { get; private set; }
		// Todo maybe move PricePlan to a user class
		// Todo make a priceplan enum
		public int PricePlan { get; set; }  /// The price plan that the user is on ( returned by Mxit at login )
        public ChallengeData ChallengeData { get; set; } /// The challenge data returned by the mxit server the first time the application is used with an account
        public int PIDErrorCode { get; set; }
        public string PrimaryTcpConnectionString { get; set; }
        public string PrimaryHttpConnectionString { get; set; }
        public string SecondaryTcpConnectionString { get; set; }
        public string SecondaryHttpConnectionString { get; set; }
        public string NationalPrefix { get; set; }
        public string InterationalPrefix { get; set; }
        public int KeepAliveTime { get; set; }
        public string UserName { get; set; }
        public string CountryCode { get; set; }
        public string Region { get; set; }
        public bool utf8Disabled { get; set; }
        
		
		private ApplicationInfo() {
            productId = string.Empty;
            DistributionCode = string.Empty;
            /*
            System.Collections.Specialized.NameValueCollection newKeyVal = new System.Collections.Specialized.NameValueCollection();
            newKeyVal.Add("HELLLO","Iaasdasd");
            System.Configuration.ConfigurationManager.AppSettings.Add( newKeyVal );
            Console.WriteLine("App stuff: " + System.Configuration.ConfigurationSettings.AppSettings["test"]);
            */

            //ReadSettings();

			// All applications are set to defaults for now
			// Todo put this setup somewhere else maybe
			// Todo this information is for a phone, which must be changed to PC information
			// Todo some of this information, such as the Distribution Code must be read from a file
			Version = "E-5.8.2-L-Nokia/N70";
			Capabilities = "w=240;h=291;dmem=1585;lmem=1;c=4096;a=256;ctyp=1217;fmem=948859;capd=4194336;utf8=false;cc=ZA;cid=0;imei=;la=0;enc=UTF8;ploc=en-US;mcc=0;mnc=0;lac=0";
			//DistributionCode = "48971339-8262-4F09-869B-899D9174D924";
			//DistributionCode = "B2353E81-943B-4EDB-8E6D-697C8B79C978";
           // ProductId = "24B2353E81-943B-4EDB-8E6D-697C8B79C9780E91A58C";
			Features = 522239;
			DialingCode = "27";
			Locale = "en";
            ChallengeData = new ChallengeData();
		}
		
		public static ApplicationInfo Instance {
			get {
				if ( application == null ) {
					application = new ApplicationInfo();
				}
				return application;
			}
		}

        public string ProductId {
            get { return productId; }
            set {
                if ( value.Length == productIdLength ) {
                    productId = value;
                    DistributionCode = productId.Substring( 2, 36 );
                }
                else {
                    DistributionCode = string.Empty;
                }
            }
        }


        public void LoadUserSettings( string userName ) {

            string productId = Utilities.ReadApplicationSetting( userName, "ProductId" );

            if ( productId != string.Empty ) {
                UserName = userName;
                ProductId = productId;
            }
            else {
                ProductId = string.Empty;
            }
            
        }

        public void SaveUserSettings() {
            Utilities.WriteApplicationSetting( UserName, "ProductId", ProductId );
        }

        public void  DoProtocolMessageError( Handler handler, ProtocolMessage protocolMessage ) {
            if ( ProtocolMessageError != null )
                ProtocolMessageError( handler, new ProtocolMessageErrorEventArgs( protocolMessage ) );
        }


  /*      private string ReadProductId( string userName ) {
           // XmlTextReader textReader = new XmlTextReader( "settings.xml" );
   

            return null;

        }

        private void WriteProductId( string userName, string ProductId ) {
            
        }*/


		
	}
}
